package com.vacomall.erp.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.velocity.app.event.ReferenceInsertionEventHandler.referenceInsertExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.sun.tools.internal.xjc.model.CBuiltinLeafInfo;
import com.vacomall.erp.ex.ProjException;
import com.vacomall.erp.model.CertificateNum;
import com.vacomall.erp.service.IQuarantineCertificateService;
import com.vacomall.erp.util.DateUtil;
import com.vacomall.erp.util.ResultCode;
import com.vacomall.erp.util.ResultUtil;

/**
 * Created by root1 on 16/8/25.
 */
@Controller
@RequestMapping(value = "/quarantineCertificate" )
public class QuarantineCertificateAction {

    @Autowired
    private IQuarantineCertificateService quarantineCertificateService;
    
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
    
    /**
     * 检疫证书新增页面
     */
    @RequestMapping(value ="plus" )
    public String plus(){
    	return "quarantineCertificate/plus";
    }
    
    /**
     * 编辑页面跳转
     * @param id
     * @return
     */
    @RequestMapping(value = "editPage" , method = RequestMethod.GET)
    public String editPage(Integer id ,ModelMap model){
    	CertificateNum cNum = quarantineCertificateService.editPage(id);
    	model.addAttribute("id",cNum.getId());
    	model.addAttribute("number",cNum.getNumber());
    	model.addAttribute("imagsId",cNum.getImagsId());
    	model.addAttribute("createTime",DateUtil.dateToString(cNum.getCreateTime()));
    	return "quarantineCertificate/dialog-normal";
    }

    /**
     * 检疫证书录入
     * @param certificateNum
     * @return
     */
    @RequestMapping(value = "/add" ,method = RequestMethod.POST)
    @ResponseBody
    public Map add(CertificateNum certificateNum) {
        int results  = quarantineCertificateService.add(certificateNum);
        Map<String, Object> map = new HashMap<String,Object>();
        map.put("certificateNum",certificateNum);
        if (results >0) {
			map.put("statusCode", "200");
			map.put("message", "这是个测试");
			map.put("tabid", "table, table-fixed");
			map.put("closeCurrent", true);
			map.put("forward", "");
			map.put("forwardConfirm", "");
			return map;
		}
        return map;
    }

    /**
     * 检疫证书修改
     * @param certificateNum
     */
    @RequestMapping(value = "/edit" , method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> edit(CertificateNum certificateNum ) {
        int results = quarantineCertificateService.edit(certificateNum);
        Map<String, Object> map = new HashMap<String,Object>();
        if (results >0) {
			map.put("statusCode", "200");
			map.put("message", "这是个测试");
			map.put("tabid", "table, table-fixed");
			map.put("closeCurrent", true);
			map.put("forward", "");
			map.put("forwardConfirm", "");
			return map;
		}
        return map;
    }


    /**
     * 检疫证书删除
     * @param number
     */
    @RequestMapping(value = "/delete" , method = RequestMethod.POST)
    @ResponseBody
    public Map<String , Object> delete(Integer id){
    	Map<String , Object> map = new HashMap<>();
    	int i = quarantineCertificateService.delete(id);
    	if (i>0) {
			map.put("statusCode", "200");
			map.put("message", "这是个测试");
			map.put("tabid", "table, table-fixed");
			map.put("closeCurrent", false);
			map.put("forward", "");
			map.put("forwardConfirm", "");
			return map;
		}
    	return null; 
    }
 
  
    /**
     * 检疫证书查询
     * @param certificateNum
     * @param pageSize  当前页大小
     * @param pageCurrent 当前页
     * @param orderField  排序字段
     * @param orderDirection   排序方向 默认asc
     * @param model
     * @return  
     */
    @RequestMapping(value = "showCertificateNum" )
    public String  showCertificateNum(CertificateNum certificateNum , 
    		@RequestParam(required = false, defaultValue = "1") int pageCurrent,
    		@RequestParam(required = false, defaultValue = "10") int pageSize ,
    		String orderField,String orderDirection,ModelMap model){
    	List<CertificateNum> list = quarantineCertificateService.showCertificate(certificateNum,pageCurrent,pageSize);
    	//用PageInfo对结果进行包装
    	PageInfo page = new PageInfo(list);
    	//测试PageInfo全部属性
    	
    	//PageInfo包含了非常全面的分页属性
    	System.out.println("当前页号  "+ page.getPageNum());
    	System.out.println("页面大小  "+  page.getPageSize());
    	System.out.println("起始行号 "+ page.getStartRow());
    	System.out.println("终止行号 "+  page.getEndRow());
    	System.out.println("总结果数 "+ page.getTotal());
    	System.out.println("总页数 "+ page.getPages());
    	System.out.println("第一页  "+  page.getFirstPage());
    	System.out.println("前一页  "+  page.getPrePage());
    	System.out.println("后一页  "+  page.getNextPage());
    	System.out.println("最后一页 "+ page.getLastPage());
    	System.out.println("是否有前一页 "+  page.isHasPreviousPage());
    	System.out.println("是否有下一页 "+ page.isHasNextPage());
    	
    	//总结果数
    	model.addAttribute("total",page.getTotal());
    	//页面大小
    	model.addAttribute("pageSize", page.getPageSize());
    	//当前页数
    	model.addAttribute("pageNum",page.getPageNum());
    	model.addAttribute("number",certificateNum.getNumber());
    	model.addAttribute("createTime",DateUtil.dateToString(certificateNum.getCreateTime()));
    	model.addAttribute("list",list);
    	return "quarantineCertificate/quarantineCertificatefixed";
    }

    

}
